#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

unordered_map<int, int> vis;

int main() {
	int n, x, y;
	cin >> n;
	vector<pii> a;
	for (int i = 1; i <= n; i++) {
		cin >> x >> y;
		if (x > y)
			swap(x, y);
		a.push_back({x, y});
	}

	sort(a.begin(), a.end(), [](pii x, pii y) {
		if (x.second != y.second)
			return x.second < y.second;
		else
			return x.second - x.first < y.second - y.first;
	});

	int cnt = 0;
	for (int i = 0; i < n; i++) {
		for (int j = a[i].first; j <= a[i].second; j++) {
			if (vis.count(j) == 0) {
				cnt++;
				vis[j] = 1;
				break;
			}
		}
	}
	cout << cnt;
	return 0;
}
